#ifndef CS_H_20141124
#define CS_H_20141124
#include <Windows.h>
#include <time.h>



#define  SPM_MIN(_A_,_B_) (((_A_) > (_B_)) ? (_B_) : (_A_)) 

#define  SPM_MAX(_A_,_B_) (((_A_) > (_B_)) ? (_A_) : (_B_)) 


#ifdef _WIN32

typedef CRITICAL_SECTION  SPM_THREAD_MUTEX_T, * PSPM_THREAD_MUTEX_T;

#define SPM_file_fopen   fopen
#define SPM_file_fclose  fclose
#define SPM_file_fread   fread
#define SPM_file_fwrite  fwrite
#define SPM_file_fflush  fflush
#define SPM_file_fseek   fseek
#define SPM_file_ftell   ftell
#define SPM_file_fseek64 _fseeki64
#define SPM_file_ftell64 _ftelli64

#define SPM_ato64    _atoi64

//#define SPM_PATH_FLAG  "\\"
#define SPM_Print64d "%I64d"

#else

typedef pthread_mutex_t    SPM_THREAD_MUTEX_T, * PSPM_THREAD_MUTEX_T;
#define SPM_file_fopen   fopen
#define SPM_file_fclose  fclose
#define SPM_file_fread   fread
#define SPM_file_fwrite  fwrite
#define SPM_file_fflush  fflush
#define SPM_file_fseek   fseek
#define SPM_file_ftell   ftell
#define SPM_file_fseek64 fseeko64
#define SPM_file_ftell64 ftello64

#define SPM_ato64    atoll


#define SPM_PATH_FLAG  "/"
#define SPM_Print64d "%ld"

#endif

class  LockBase
{
public:
    LockBase();
    virtual ~LockBase();
    virtual void Lock() = 0;
    virtual void UnLock() = 0;
};

class  DerivedLock : public LockBase
{
public:
    DerivedLock();
    virtual ~DerivedLock();
    virtual void Lock();
    virtual void UnLock();
    virtual void Destroy();
private:
    SPM_THREAD_MUTEX_T m_cs;
};


class  SafeLock
{
public:
    SafeLock();
    SafeLock(LockBase* pLock);
    virtual ~SafeLock();
private:
    LockBase* m_pLock;
};


class CNGLock
{
	CRITICAL_SECTION m_CS;
public:
	CNGLock(){::InitializeCriticalSection( &m_CS );}

	~CNGLock(){::DeleteCriticalSection( &m_CS );}
private:
	CNGLock( const CNGLock & );           // not implemented
	CNGLock& operator=( const CNGLock& ); // not implemented
public:
	/// wait for ownership
	void Lock(){EnterCriticalSection( &m_CS );}
	/// release ownership
	void Unlock(){::LeaveCriticalSection( &m_CS );}
};
#endif

